package com.zui.util.mail;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.mail.MailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.zui.entity.party.Person;
import com.zui.security.User;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;

/**
 * This is a normal implementation to the MailManager class. for asynchronous
 * jms implementation using MailManagerJmsImpl
 * 
 * @author zzuizui
 */
public class MailManagerImpl implements MailManager {
	protected transient final Log log = LogFactory.getLog(getClass());

	private JavaMailSenderImpl mailSender;
	private Configuration freemarkerConfiguration;
	private String signUpConfirmationMailTemplate;
	private String resetPasswordMailTemplate;
	

	@Override
	public void sendMailMessage(MailMessage mailMessage) {

	}

	@Override
	public void sendSignUpConfirmationMessage(User user) {
		try {
			// send mail
			mailSender.setHost("smtp.126.com");
			mailSender.setUsername("zzuizui@126.com");
			mailSender.setPassword("GhostHolewuqiwi");
			mailSender.setDefaultEncoding("UTF-8");

			/****** Add stuff to the model for inserting into the template ******/
			final Map<String, User> model = new HashMap<String, User>();
			model.put("user", user);

			/****** Merge the model into the template ******/
			final String result;
			result = FreeMarkerTemplateUtils
					.processTemplateIntoString(freemarkerConfiguration
							.getTemplate(signUpConfirmationMailTemplate), model);

			/****** Construct and send the message ******/
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			// use the true flag to indicate you need a multipart message
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom("zzuizui@126.com");
			helper.setTo(user.getEmail());
			// use the true flag to indicate the text included is HTML
			helper.setText(result, true);
			
			//TODO: add multi-language support.
			helper.setSubject("Confirm your email address");

			mailSender.send(mimeMessage);

		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	@Override
	public void sendResetPasswordMessage(User user) {
		try {
			// send mail
			mailSender.setHost("smtp.126.com");
			mailSender.setUsername("zzuizui@126.com");
			mailSender.setPassword("GhostHolewuqiwi");
			mailSender.setDefaultEncoding("UTF-8");

			/****** Add stuff to the model for inserting into the template ******/
			final Map<String, User> model = new HashMap<String, User>();
			model.put("user", user);

			/****** Merge the model into the template ******/
			final String result;
			result = FreeMarkerTemplateUtils
					.processTemplateIntoString(freemarkerConfiguration
							.getTemplate(resetPasswordMailTemplate), model);

			/****** Construct and send the message ******/
			MimeMessage mimeMessage = mailSender.createMimeMessage();
			// use the true flag to indicate you need a multipart message
			MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
			helper.setFrom("zzuizui@126.com");
			helper.setTo(user.getEmail());
			// use the true flag to indicate the text included is HTML
			helper.setText(result, true);
			
			//TODO: add multi-language support.
			helper.setSubject("Resetting your iobike password");

			mailSender.send(mimeMessage);

		} catch (MessagingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	

	public JavaMailSenderImpl getMailSender() {
		return mailSender;
	}

	public void setMailSender(JavaMailSenderImpl mailSender) {
		this.mailSender = mailSender;
	}

	public Configuration getFreemarkerConfiguration() {
		return freemarkerConfiguration;
	}

	public void setFreemarkerConfiguration(Configuration freemarkerConfiguration) {
		this.freemarkerConfiguration = freemarkerConfiguration;
	}

	public String getSignUpConfirmationMailTemplate() {
		return signUpConfirmationMailTemplate;
	}

	public void setSignUpConfirmationMailTemplate(
			String signupConfirmationMailTemplate) {
		this.signUpConfirmationMailTemplate = signupConfirmationMailTemplate;
	}

	public String getResetPasswordMailTemplate() {
		return resetPasswordMailTemplate;
	}

	public void setResetPasswordMailTemplate(String resetPasswordMailTemplate) {
		this.resetPasswordMailTemplate = resetPasswordMailTemplate;
	}

}
